package pbbltobpel.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import pbbltobpel.model.steps.Step;

public class Process implements IXmlWriter {
	private String name;
	private String targetNamespace;
	private boolean suppressJoinFailure;
	private HashMap<String, String> namespaces;
	private List<Import> imports;
	private boolean exitOnStandardFault;
	private List<PartnerLink> partnerLinks;
	private List<Variable> variables;
	private List<Step> steps;
	
	public Process(){
		namespaces = new HashMap<String, String>();
		namespaces.put("bpel", "http://docs.oasis-open.org/wsbpel/2.0/process/executable");
		setImports(new ArrayList<Import>());
		partnerLinks = new ArrayList<PartnerLink>();
		setVariables(new ArrayList<Variable>());
		setSteps(new ArrayList<Step>());
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getTargetNamespace() {
		return targetNamespace;
	}
	public void setTargetNamespace(String argetNamespace) {
		this.targetNamespace = argetNamespace;
	}
	public boolean isSuppressJoinFailure() {
		return suppressJoinFailure;
	}
	public void setSuppressJoinFailure(boolean suppressJoinFailure) {
		this.suppressJoinFailure = suppressJoinFailure;
	}
	public HashMap<String, String> getNamespaces() {
		return namespaces;
	}
	public void setNamespaces(HashMap<String, String> namespaces) {
		this.namespaces = namespaces;
	}

	public boolean isExitOnStandardFault() {
		return exitOnStandardFault;
	}

	public void setExitOnStandardFault(boolean exitOnStandardFault) {
		this.exitOnStandardFault = exitOnStandardFault;
	}

	public List<Import> getImports() {
		return imports;
	}

	public void setImports(List<Import> imports) {
		this.imports = imports;
	}

	public List<PartnerLink> getPartnerLinks() {
		return partnerLinks;
	}

	public void setPartnerLinks(List<PartnerLink> partnerLinks) {
		this.partnerLinks = partnerLinks;
	}
	public List<Variable> getVariables() {
		return variables;
	}

	public void setVariables(List<Variable> variables) {
		this.variables = variables;
	}

	public List<Step> getSteps() {
		return steps;
	}

	public void setSteps(List<Step> steps) {
		this.steps = steps;
	}
	
	@Override
	public Element getXmlElement(Document doc) {
		Element element = doc.createElement("bpel:process");
		element.setAttribute("name", name);
		element.setAttribute("targetNamespace", targetNamespace);
		element.setAttribute("suppressJoinFailure", suppressJoinFailure ? "yes" : "no");
		for(String key : namespaces.keySet())
			element.setAttribute("xmlns:" + key, namespaces.get(key));
		for(Import imp : imports)
			element.appendChild(imp.getXmlElement(doc));
		Element parrtnersElement = doc.createElement("bpel:partnerLinks");
		for(PartnerLink link : partnerLinks)
			parrtnersElement.appendChild(link.getXmlElement(doc));
		element.appendChild(parrtnersElement);
		Element variableElement = doc.createElement("bpel:variables");
		element.appendChild(variableElement);
		for(Variable variable : variables)
			variableElement.appendChild(variable.getXmlElement(doc));
		Element sequence = doc.createElement("bpel:sequence");
		sequence.setAttribute("name", "main");
		for(Step step : steps)
			sequence.appendChild(step.getXmlElement(doc));
		element.appendChild(sequence);
		return element;
	}
	
	public Document getDocument() throws ParserConfigurationException
	{
		DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
        Document doc = docBuilder.newDocument();
        
        doc.appendChild(getXmlElement(doc));
        return doc;
	}


}
